package com.wyp168.leetcode.harfsearch;

import java.util.Arrays;

/**
 * @ClassName Search01
 * @Description TODO
 * @Author wyp168
 * @Date 2022/6/28 18:34
 */
public class Search01 {
    public static void main(String[] args) {
        int[] nums = {-1,0,3,5,9,12};
        int target = 12;
        System.out.println(search(nums,target));
    }

    /**
     * @description: 二分查找指定升序序列中的指定值
     * @param: null
     * @return:
     * @author wyp168
     * @date: 2022/6/28 18:34
     */

    public static int search(int[] nums, int target) {
        Arrays.sort(nums);
        int start = 0;
        int end = nums.length - 1;
        int middle = 0;

        while (start <= end) {
            middle = start + (end - start) / 2;
            if (nums[middle] > target)
                end = middle - 1;
            if (nums[middle] < target)
                start = middle + 1;
            if (nums[middle] == target)
                return middle;
        }
        return -1;
    }
}
